home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Interfaces&Libraries / Universal / Interfaces / CIncludes / Math64.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-17  |  11.1 KB  |  377 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        Math64.h
  3.  
  4.      Contains:    64-bit integer math Interfaces.
  5.  
  6.      Version:    Technology:    System 7.5
  7.                  Release:    Universal Interfaces 3.2
  8.  
  9.      Copyright:    © 1994-1998 by Apple Computer, Inc., all rights reserved
  10.  
  11.      Bugs?:        For bug reports, consult the following page on
  12.                  the World Wide Web:
  13.  
  14.                      http://developer.apple.com/bugreporter/
  15.  
  16. */
  17. #ifndef __MATH64__
  18. #define __MATH64__
  19.  
  20. #ifndef __CONDITIONALMACROS__
  21. #include <ConditionalMacros.h>
  22. #endif
  23. #ifndef __MACTYPES__
  24. #include <MacTypes.h>
  25. #endif
  26.  
  27.  
  28.  
  29. #if PRAGMA_ONCE
  30. #pragma once
  31. #endif
  32.  
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36.  
  37. #if PRAGMA_IMPORT
  38. #pragma import on
  39. #endif
  40.  
  41. #if PRAGMA_STRUCT_ALIGN
  42.     #pragma options align=mac68k
  43. #elif PRAGMA_STRUCT_PACKPUSH
  44.     #pragma pack(push, 2)
  45. #elif PRAGMA_STRUCT_PACK
  46.     #pragma pack(2)
  47. #endif
  48.  
  49.  
  50. /*--------------------------------------------------------------------------------
  51.                 These routines are intended to provide C software support for
  52.                 64 bit integer types.  Their behavior should mimic anticipated
  53.                 64 bit hardware. This implementation should replace use of the
  54.                 "wide" type found in PowerPC.
  55.  
  56.     The following routines are available for performing math on 64-bit integers:
  57.     
  58.     S64Max
  59.                 Returns the largest representable SInt64.
  60.     S64Min
  61.                 Returns the smallest (i.e. most negative) SInt64.  Note: the negative
  62.                 (absolute value) of this number is not representable in an SInt64.
  63.                 That means that S64Negate(S64Min) is not representable (in fact,
  64.                 it returns S64Min).
  65.     S64Add
  66.                 Adds two integers, producing an integer result.  If an overflow
  67.                 occurs the result is congruent mod (2^64) as if the operands and
  68.                 result were unsigned.  No overflow is signaled.
  69.     
  70.     S64Subtract
  71.                 Subtracts two integers, producing an integer result.  If an overflow
  72.                 occurs the result is congruent mod (2^64) as if the operands and
  73.                 result were unsigned.  No overflow is signaled.
  74.  
  75.     S64Negate
  76.                 Returns the additive inverse of a signed number (i.e. it returns
  77.                 0 - the number).  S64Negate (S64Min) is not representable (in fact,
  78.                 it returns S64Min).
  79.     
  80.     S64Absolute
  81.                 Returns the absolute value of the number (i.e. the number if
  82.                 it is positive, or 0 - the number if it is negative).
  83.                 See S64Negate above.
  84.                 
  85.     S64Multiply
  86.                 Multiplies two signed numbers, producing a signed result.  Overflow
  87.                 is ignored and the low-order part of the product is returned.  The
  88.                 sign of the result is not guaranteed to be correct if the magnitude
  89.                 of the product is not representable.
  90.                 
  91.     S64Div
  92.                 Divides dividend by divisor, returning the quotient.  
  93.  
  94.     S64Divide
  95.                 Divides dividend by divisor, returning the quotient.  The remainder
  96.                 is returned in *remainder if remainder (the pointer) is non-NULL.
  97.                 The sign of the remainder is the same as the sign of the dividend
  98.                 (i.e. it takes the absolute values of the operands, does the division,
  99.                 then fixes the sign of the quotient and remainder).  If the divisor
  100.                 is zero, then S64Max() will be returned (or S64Min() if the dividend
  101.                 is negative), and the remainder will be the dividend; no error is
  102.                 reported.
  103.     
  104.     S64Set
  105.                 Given an SInt32, returns an SInt64 with the same value.  Use this
  106.                 routine instead of coding 64-bit constants (at least when the
  107.                 constant will fit in an SInt32).
  108.     
  109.     S64SetU
  110.                 Given a UInt32, returns a SInt64 with the same value.
  111.     
  112.     S64Compare
  113.                 Given two signed numbers, left and right, returns an
  114.                 SInt32 that compares with zero the same way left compares with
  115.                 right.  If you wanted to perform a comparison on 64-bit integers
  116.                 of the form:
  117.                         operand_1 <operation> operand_2
  118.                 then you could use an expression of the form:
  119.                         xxxS64Compare(operand_1,operand_2) <operation> 0
  120.                 to test for the same condition.
  121.                 
  122.                 CAUTION: DO NOT depend on the exact value returned by this routine.
  123.                 Only the sign (i.e. positive, zero, or negative) of the result is
  124.                 guaranteed.
  125.  
  126.     S64And, S64Or, S64Eor and S64Not
  127.     
  128.                 Return Boolean (1 or 0) depending on the outcome of the logical
  129.                 operation.
  130.  
  131.     S64BitwiseAnd, S64BitwiseOr, S64BitwiseEor and S64BitwiseNot
  132.     
  133.                 Return the Bitwise result.
  134.                 
  135.     S64ShiftRight and S64ShiftLeft
  136.     
  137.                 The lower 7 bits of the shift argument determines the amount of 
  138.                 shifting.  S64ShiftRight is an arithmetic shift while U64ShiftRight
  139.                 is a logical shift.
  140.  
  141.     SInt64ToLongDouble
  142.                 
  143.                 Converts SInt64 to long double.  Note all SInt64s fit exactly into 
  144.                 long doubles, thus, the binary -> decimal conversion routines
  145.                 in fp.h can be used to achieve SInt64 -> long double -> decimal
  146.                 conversions.
  147.                 
  148.     LongDoubleToSInt64
  149.     
  150.                 Converts a long double to a SInt64.  Any decimal string that fits
  151.                 into a SInt64 can be converted exactly into a long double, using the
  152.                 conversion routines found in fp.h.  Then this routine can be used
  153.                 to complete the conversion to SInt64.
  154.                 
  155.     SInt64ToWide
  156.     
  157.                 Converts a SInt64 to a wide struct.  If SInt64 is implemented
  158.                 as a typedef of wide, the marco does nothing. If SInt64 is 
  159.                 implememnted as a long long, it casts the long long into a 
  160.                 wide struct.
  161.     
  162.     WideToSInt64
  163.     
  164.                 Converts a wide struct into a SInt64.  If SInt64 is implemented
  165.                 as a typedef of wide, the marco does nothing. If SInt64 is 
  166.                 implememnted as a long long, it reads the struct into a long long.
  167.     
  168.     
  169.     The corresponding UInt64 routines are also included.
  170.     
  171. --------------------------------------------------------------------------------*/
  172.  
  173.  
  174. #if TYPE_LONGLONG 
  175.  
  176. #define S64Max() 9223372036854775807
  177. #define S64Min() (-S64Max() - 1)
  178. #define S64Add(x, y) ((x) + (y))
  179. #define S64Subtract(x, y) ((x) - (y))
  180. #define S64Negate(x) (-(x))
  181. #define S64Absolute(x) absll((x))
  182. #define S64Multiply(x, y) ((x) * (y))
  183. #define S64Div(x, y) ((x) / (y))
  184. #define S64Mod(x, y) ((x) % (y))
  185. #define S64Set(x) ((SInt64) (x))
  186. #define S64SetU(x) ((SInt64) (x))
  187. #define S32Set(x) ((SInt32) (x))
  188. #define S64Compare(x, y) ((int)((x) - (y)))
  189. #define S64And(x, y) ((Boolean)((x) && (y)))
  190. #define S64Or(x, y) ((Boolean)((x) || (y)))
  191. #define S64Eor(x, y) ((Boolean)((x) ^ (y)))
  192. #define S64Not(x) ((Boolean)(!(x)))
  193. #define S64BitwiseAnd(x, y) ((x) & (y))
  194. #define S64BitwiseOr(x, y) ((x) | (y))
  195. #define S64BitwiseEor(x, y) (((x) & (~(y))) | ((~(x)) & (y)))
  196. #define S64BitwiseNot(x) (~(x))
  197. #define S64ShiftRight(x, y) ((x) >> (y))
  198. #define S64ShiftLeft(x, y) ((x) << (y))
  199. #define SInt64ToLongDouble(x) ((long double)(x))
  200. #define LongDoubleToSInt64(x) ((SInt64)(x))
  201.  
  202. #define U64Max() 0xffffffffffffffff
  203. #define U64Add(x, y) ((x) + (y))
  204. #define U64Subtract(x, y) ((x) - (y))
  205. #define U64Multiply(x, y) ((x) * (y))
  206. #define U64Div(x, y) ((x) / (y))
  207. #define U64Mod(x, y) ((x) % (y))
  208. #define U64Set(x) ((UInt64) (x))
  209. #define U64SetU(x) ((UInt64) (x))
  210. #define U32SetU(x) ((UInt32) (x))
  211. #define U64Compare(x, y) ((int)((x) - (y)))
  212. #define U64And(x, y) ((Boolean)((x) && (y)))
  213. #define U64Or(x, y) ((Boolean)((x) || (y)))
  214. #define U64Eor(x, y) ((Boolean)((x) ^ (y)))
  215. #define U64Not(x) ((Boolean)(!(x)))
  216. #define U64BitwiseAnd(x, y) ((x) & (y))
  217. #define U64BitwiseOr(x, y) ((x) | (y))
  218. #define U64BitwiseEor(x, y) (((x) & (~(y))) | ((~(x)) & (y)))
  219. #define U64BitwiseNot(x) (~(x))
  220. #define U64ShiftRight(x, y) ((x) >> (y))
  221. #define U64ShiftLeft(x, y) ((x) << (y))
  222. #define UInt64ToLongDouble(x) ((long double)(x))
  223. #define LongDoubleToUInt64(x) ((UInt64)(x))
  224. #define UInt64ToSInt64(x) ((SInt64)(x))
  225. #define SInt64ToUInt64(x) ((UInt64)(x))
  226.  
  227. #else  
  228.  
  229. EXTERN_API_C( SInt64 ) S64Max(void );
  230.  
  231. EXTERN_API_C( SInt64 ) S64Min(void );
  232.  
  233. EXTERN_API_C( SInt64 ) S64Add(SInt64 x, SInt64 y);
  234.  
  235. EXTERN_API_C( SInt64 ) S64Subtract(SInt64 left, SInt64 right);
  236.  
  237. EXTERN_API_C( SInt64 ) S64Negate(SInt64 value);
  238.  
  239. EXTERN_API_C( SInt64 ) S64Absolute(SInt64 value);
  240.  
  241. EXTERN_API_C( SInt64 ) S64Multiply(SInt64 xparam, SInt64 yparam);
  242.  
  243. #define S64Div(dividend, divisor) S64Divide(dividend, divisor, NULL)
  244.  
  245. EXTERN_API_C( SInt64 ) S64Divide(SInt64 dividend, SInt64 divisor, SInt64 *remainder);
  246.  
  247. EXTERN_API_C( SInt64 ) S64Set(SInt32 value);
  248.  
  249. EXTERN_API_C( SInt64 ) S64SetU(UInt32 value);
  250.  
  251. EXTERN_API_C( SInt32 ) S32Set(SInt64 value);
  252.  
  253. EXTERN_API_C( int ) S64Compare(SInt64 left, SInt64 right);
  254.  
  255. EXTERN_API_C( Boolean ) S64And(SInt64 left, SInt64 right);
  256.  
  257. EXTERN_API_C( Boolean ) S64Or(SInt64 left, SInt64 right);
  258.  
  259. EXTERN_API_C( Boolean ) S64Eor(SInt64 left, SInt64 right);
  260.  
  261. EXTERN_API_C( Boolean ) S64Not(SInt64 value);
  262.  
  263. EXTERN_API_C( SInt64 ) S64BitwiseAnd(SInt64 left, SInt64 right);
  264.  
  265. EXTERN_API_C( SInt64 ) S64BitwiseOr(SInt64 left, SInt64 right);
  266.  
  267. EXTERN_API_C( SInt64 ) S64BitwiseEor(SInt64 left, SInt64 right);
  268.  
  269. EXTERN_API_C( SInt64 ) S64BitwiseNot(SInt64 value);
  270.  
  271. EXTERN_API_C( SInt64 ) S64ShiftRight(SInt64 value, UInt32 shift);
  272.  
  273. EXTERN_API_C( SInt64 ) S64ShiftLeft(SInt64 value, UInt32 shift);
  274.  
  275. /*
  276.     "long double" means 128 bit type on PowerPC and 80-bit type on 68K
  277. */
  278. EXTERN_API_C( long double ) SInt64ToLongDouble(SInt64 value);
  279.  
  280. EXTERN_API_C( SInt64 ) LongDoubleToSInt64(long double value);
  281.  
  282. EXTERN_API_C( UInt64 ) U64Max(void );
  283.  
  284. EXTERN_API_C( UInt64 ) U64Add(UInt64 x, UInt64 y);
  285.  
  286. EXTERN_API_C( UInt64 ) U64Subtract(UInt64 left, UInt64 right);
  287.  
  288. EXTERN_API_C( UInt64 ) U64Multiply(UInt64 xparam, UInt64 yparam);
  289.  
  290. #define U64Div(dividend, divisor) U64Divide(dividend, divisor, NULL)
  291.  
  292. EXTERN_API_C( UInt64 ) U64Divide(UInt64 dividend, UInt64 divisor, UInt64 *remainder);
  293.  
  294. EXTERN_API_C( UInt64 ) U64Set(SInt32 value);
  295.  
  296. EXTERN_API_C( UInt64 ) U64SetU(UInt32 value);
  297.  
  298. EXTERN_API_C( UInt32 ) U32SetU(UInt64 value);
  299.  
  300. EXTERN_API_C( int ) U64Compare(UInt64 left, UInt64 right);
  301.  
  302. EXTERN_API_C( Boolean ) U64And(UInt64 left, UInt64 right);
  303.  
  304. EXTERN_API_C( Boolean ) U64Or(UInt64 left, UInt64 right);
  305.  
  306. EXTERN_API_C( Boolean ) U64Eor(UInt64 left, UInt64 right);
  307.  
  308. EXTERN_API_C( Boolean ) U64Not(UInt64 value);
  309.  
  310. EXTERN_API_C( UInt64 ) U64BitwiseAnd(UInt64 left, UInt64 right);
  311.  
  312. EXTERN_API_C( UInt64 ) U64BitwiseOr(UInt64 left, UInt64 right);
  313.  
  314. EXTERN_API_C( UInt64 ) U64BitwiseEor(UInt64 left, UInt64 right);
  315.  
  316. EXTERN_API_C( UInt64 ) U64BitwiseNot(UInt64 value);
  317.  
  318. EXTERN_API_C( UInt64 ) U64ShiftRight(UInt64 value, UInt32 shift);
  319.  
  320. EXTERN_API_C( UInt64 ) U64ShiftLeft(UInt64 value, UInt32 shift);
  321.  
  322. /*
  323.     "long double" means 128 bit type on PowerPC and 80-bit type on 68K
  324. */
  325. EXTERN_API_C( long double ) UInt64ToLongDouble(UInt64 value);
  326.  
  327. EXTERN_API_C( UInt64 ) LongDoubleToUInt64(long double value);
  328.  
  329. EXTERN_API_C( SInt64 ) UInt64ToSInt64(UInt64 value);
  330.  
  331. EXTERN_API_C( UInt64 ) SInt64ToUInt64(SInt64 value);
  332.  
  333. #endif /* TYPE_LONGLONG */
  334.  
  335. /* 
  336.     Functions to convert between [Unsigned]Wide and [S|U]Int64 types.
  337.     
  338.     These functions are necessary if source code which uses both
  339.     wide and SInt64 is to compile under a compiler that supports
  340.     long long.
  341. */
  342. #if TYPE_LONGLONG 
  343.     #define SInt64ToWide(x)         (*((wide*)(&x)))
  344.     #define WideToSInt64(x)         (*((SInt64*)(&x)))
  345.     #define UInt64ToUnsignedWide(x) (*((UnsignedWide*)(&x)))
  346.     #define UnsignedWideToUInt64(x) (*((UInt64*)(&x)))
  347. #else
  348.     #define SInt64ToWide(x)         (x)
  349.     #define WideToSInt64(x)         (x)
  350.     #define UInt64ToUnsignedWide(x) (x)
  351.     #define UnsignedWideToUInt64(x) (x)
  352. #endif
  353.  
  354.  
  355.  
  356.  
  357. #if PRAGMA_STRUCT_ALIGN
  358.     #pragma options align=reset
  359. #elif PRAGMA_STRUCT_PACKPUSH
  360.     #pragma pack(pop)
  361. #elif PRAGMA_STRUCT_PACK
  362.     #pragma pack()
  363. #endif
  364.  
  365. #ifdef PRAGMA_IMPORT_OFF
  366. #pragma import off
  367. #elif PRAGMA_IMPORT
  368. #pragma import reset
  369. #endif
  370.  
  371. #ifdef __cplusplus
  372. }
  373. #endif
  374.  
  375. #endif /* __MATH64__ */
  376.  
  377.